package edu.cmu.hcii.modalnodes.animation;
import java.util.logging.Logger;
import processing.core.PApplet;
import processing.core.PVector;
import edu.cmu.hcii.modalnodes.Controls;

public class Particle {
	private final Logger log = Logger.getLogger(Controls.class.getName());
	private PApplet parent;
	private PVector location;
	private PVector velocity;
	  //---------------
	private PVector acceleration;
	private float topspeed;
	private double multiplyer;
	private int start;
	private int r, g, b;
	
	public Particle(PApplet parent, int r, int g, int b) {
		this.parent = parent;
		this.r = r;
		this.g = g;
		this.b = b;
		//create start location
		location = new PVector(parent.random(parent.width), parent.random(parent.height));
		//create velocity(director over time)
		velocity = new PVector(parent.random((float) -4.5,(float) 4.5), parent.random((float)-4.5, (float)4.5)); //*** reset to 0 below
		topspeed = 10;
		multiplyer = 0.8;
		start = 0;
	}
	
	public void update(float yHeight, float x, float y) {
	    //println(yHeight);
	    
	    if (yHeight > 380) {
	        //println("floating"); 
	        //velocity = new PVector(random(-2.5, 2.5), random(-2.5, 2.5)); //*** reset to 0 below
	        location.add(velocity); 
	    } else {
	        if (yHeight < 300 && yHeight > 215) {
	          multiplyer = 1;
	          topspeed = 10;
	          //println("300");
	        } else if (yHeight < 215 && yHeight > 185) {
	          multiplyer = 2;
	          topspeed = 10;
	          //println("215");
	        } else if (yHeight < 185 && yHeight > 150) {
	          multiplyer = 15;
	          topspeed = 30;
	          //println("185");
	        } else {
	          multiplyer = 0.8;
	          topspeed = 10;
	          //println("else");
	        }
	      
	      //println("attractor");   
	      //------------------
	      PVector mouse = new PVector(x, y); //mouse location
	      //find a vector pointing from the particle to the mouse
	      PVector acceleration = PVector.sub(mouse, location); 
	      // Set magnitude of acceleration
	      //reset from 0 to 1
	      acceleration.normalize();
	      //set acceleration factor
	      acceleration.mult((float) multiplyer);
	      //Velocity changes to acceleration
	      velocity.add(acceleration);
	      //limit the velocity by top speed
	      velocity.limit(topspeed);
	      //------------------ 
	      // Location changes by velocity
	      location.add(velocity);
	    }
	}
	
	public void display() {
	    parent.noStroke();
	    parent.fill(r, g, b);
	    parent.ellipse(location.x,location.y,(float) 3,(float) 3);
	}
	
	public void checkEdges() {
		    //if the particle reaches the edge of the screen flip its velocity  
		    if ((location.x > parent.width) || (location.x < 0)) {
		      velocity.x = velocity.x * -1;
		    }
		    if ((location.y > parent.height) || (location.y < 0)) {
		      velocity.y = velocity.y * -1;
		    }
	}
	
}






